﻿using System;
using System.Windows.Automation;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows;

namespace AutoWinUI 
{
    public class Desktop
    {
        #region Non-UI Automation part
        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        [DllImport("user32.dll")]
        static extern byte MapVirtualKey(byte wCode, int wMap);
        #endregion

        /// <summary>
        /// Click Start Icon and show Start menu
        /// </summary>
        public static void ClickStartButton()
        {
            AutomationElement TaskBar = FindTaskBar();

            AutoElement ele = new AutoElement();
            ele.AutomationID = "";
            ele.ClassName = "Button";
            ele.ParentElement = TaskBar;

            AutomationElement StartButton = null;

            try
            {
                StartButton = ele.FindFirst(SearchScope.Children);
                PatternAction.InvokePatternInvokeAction(StartButton);
            }
            catch
            { // do this
            
            }

        }// end of function

        private static AutomationElement FindTaskBar()
        {
            AutomationElement Desktop = AutomationElement.RootElement;
            AutoElement ele = new AutoElement();
            ele.AutomationID = "";
            ele.ClassName = "Shell_TrayWnd";
            ele.ParentElement = Desktop;

            AutomationElement TaskBar = null;

            try
            {
                TaskBar = ele.FindFirst(SearchScope.Children);
            }
            catch
            { }

            if (TaskBar == null)
                throw new ElementNotAvailableException("TaskBar is not found!");

            return TaskBar;
        }

        /// <summary>
        /// Launch the program installed in the system 
        /// </summary>
        /// <param name="CommandName">Program name</param>
        public static void LaunchProgram(string CommandName)
        {
            ClickStartButton();

            AutomationElement Desktop = AutomationElement.RootElement;
            AutomationElement SearchProgram = null;

            AutoElement ele = new AutoElement();
            ele.AutomationID = "";
            ele.ClassName = "DV2ControlHost";
            ele.ParentElement = Desktop;

            try
            {
                SearchProgram = ele.FindFirst(SearchScope.Children);
            }
            catch
            { //do something here
            }

            if ( SearchProgram == null )
                throw new ElementNotAvailableException("SearchProgram Window is not found!");

            AutomationElement SearchText = null;
            ele.AutomationID = "SearchEditBox";
            ele.ClassName = "SearchEditBox";
            ele.ParentElement = SearchProgram;

            try
            {
                SearchText = ele.FindFirst(SearchScope.Descendants);
            }
            catch
            { }
            
            if ( SearchText == null)
                throw new ElementNotAvailableException("SearchText textbox is not found!");

            PatternAction.ValuePatternSetValue(SearchText, CommandName);

            //send a enter to launch program
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");

        }

        /// <summary>
        /// 
        /// </summary>
        public static void ShowDesktop()
        {
            //press down  ( LeftWin + D )
            //keybd_event((byte)0x5B,0, 0, 0);
            //System.Threading.Thread.Sleep(100);
            //keybd_event((byte)0x44,0, 0, 0);
            //System.Threading.Thread.Sleep(100);
            ////release up
            //keybd_event((byte)0x44, 0, 0x2, 0);
            //System.Threading.Thread.Sleep(100);
            //keybd_event((byte)0x5B, 0, 0x2, 0);
            //System.Threading.Thread.Sleep(100);

            AutomationElement TaskBar = FindTaskBar();
            AutomationElement ShowDesktopButton = null;
            AutoElement ele = new AutoElement();
            ele.AutomationID = "307";
            ele.ClassName = "TrayShowDesktopButtonWClass";
            ele.ParentElement = TaskBar;

            try
            {
                ShowDesktopButton = ele.FindFirst(SearchScope.Descendants);
            }
            catch
            { }

            //PatternAction.InvokePatternInvokeAction(ShowDesktopButton);
            Point p = new Point();
            if (ele.TryGetClickablePoint(ShowDesktopButton, out p))
            {
                ele.MouseLeftClick((int)p.X, (int)p.Y); 
            }
            else
            {
                throw new Exception("Cannot get clickable point of ShowDesktop element");
            }

            
        }

    }
}
